const baseUrl = getApp().globalData.baseUrl

const axios = params => {
  return new Promise((resolve, reject) => {
    let data = Object.assign(params, {url: baseUrl + params.url})
    wx.request({
      ...data,
      success: result => {
        resolve(result)
      },
      fail: err => {
        reject(err)
      }
    })
  })
}

const request = {
  get:(url, data, header) => {
    const app = getApp();
    let headers = Object.assign(header || {}, {'content-type': 'application/json','systemNo': 'khyMobile'}) 
    if (app.globalData.userInfo && app.globalData.userInfo.token) {
      headers = Object.assign(headers, {'Access-Token': app.globalData.userInfo.token})
    }
    return axios({
      url:url,
      data: data || {},
      method: 'GET',
      header: headers
    })
  },
  post: (url, data, header) => {
    const app = getApp();
    let headers = Object.assign(header || {}, {'content-type': 'application/json','systemNo': 'khyMobile'}) 
    if (app.globalData.userInfo && app.globalData.userInfo.token) {
      headers = Object.assign(headers, {'Access-Token': app.globalData.userInfo.token})
    }
    return axios({
      url: url,
      data: data || {},
      method: 'POST',
      header: headers
    })
  }
}

module.exports = request